package cn.anc.aonicardv.module.ui.my;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import androidx.annotation.Nullable;
import cn.anc.aonicardv.Configs;
import cn.anc.aonicardv.Constant;
import cn.anc.aonicardv.MyApplication;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.bean.LanguageSwitchBean;
import cn.anc.aonicardv.module.adpter.my.SwitchLanguageAdapter;
import cn.anc.aonicardv.module.ui.MainActivity;
import cn.anc.aonicardv.module.ui.base.BaseActivity;
import cn.anc.aonicardv.util.AppUtils;
import cn.anc.aonicardv.util.LanguageConstants;
import cn.anc.aonicardv.util.LogUtil;
import cn.anc.aonicardv.util.SpUtils;

/**
 * Created by yangdai on 2017/12/5.
 */

public class SwitchAppLanuageActivity extends BaseActivity implements View.OnClickListener {
    private TextView titleTv;
    private ListView mLv;
    private SwitchLanguageAdapter mAdapter;
    private List<LanguageSwitchBean> mDataList = new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_switch_app_lanuage);
        titleTv = (TextView) findViewById(R.id.tv_title);
        titleTv.setText(getString(R.string.my_switch_app_language));
        findViewById(R.id.tv_back).setOnClickListener(this);
        mLv = (ListView) findViewById(R.id.lv_switch_language);
        mAdapter = new SwitchLanguageAdapter(this);
        mLv.setAdapter(mAdapter);
        mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                int sPosition =   mAdapter.getSelectPosition();
                SpUtils.putBoolean(Constant.SpKeyParam.IS_USER_SETTING_LANUAGE,true);
                if (sPosition == position){
                    finish();
                    return;
                }
                SpUtils.putString(Constant.SpKeyParam.APP_LANUAGE, mAdapter.getItem(position).getNameType());
                String mSelectLanguage =   SpUtils.getString(Constant.SpKeyParam.APP_LANUAGE, Configs.DEFUTL_LANGUAGE);
                AppUtils.updateAppLanguage(MyApplication.getInstance(), new Locale(mSelectLanguage));
                setDefaultSelectLanguage();
//                //地图跟随语言变动,当不为简体或者繁体，默认强制设置为google地图
                if (Configs.DEFUTL_SELECT_MAP ){
                    if (!(mSelectLanguage.equals(LanguageConstants.SIMPLIFIED_CHINESE)||mSelectLanguage.equals(LanguageConstants.TRADITIONAL_CHINESE))
                            ){
                        MyApplication.typeMap =Constant.OfflineMapParam.GOOGLE_MAP;
                        SpUtils.putInt(Constant.SpKeyParam.TYPE_MAP, Constant.OfflineMapParam.GOOGLE_MAP);
                    }else{
                        MyApplication.typeMap =Constant.OfflineMapParam.GAODE_MAP;
                        SpUtils.putInt(Constant.SpKeyParam.TYPE_MAP, Constant.OfflineMapParam.GAODE_MAP);
                    }
                }
                Intent intent = new Intent(SwitchAppLanuageActivity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);

            }
        });

//        通过常量配置
        String[] defultLanguage = Configs.DEFUTL_LANGUAGES.trim().split(",");
        if (defultLanguage.length<=0){
            return;
        }
        for (int i = 0; i < defultLanguage.length; i++) {
            LanguageSwitchBean bean = new LanguageSwitchBean();
            bean.setSelect(false);
            if (defultLanguage[i].equals(LanguageConstants.SIMPLIFIED_CHINESE)) {
                bean.setName(LanguageConstants.SIMPLIFIED_CHINESE_VALUE);
                bean.setNameType(LanguageConstants.SIMPLIFIED_CHINESE);
            } else if (defultLanguage[i].equals(LanguageConstants.ENGLISH)) {
                bean.setName(LanguageConstants.ENGLISH_VALUE);
                bean.setNameType(LanguageConstants.ENGLISH);
            } else if (defultLanguage[i].equals(LanguageConstants.TRADITIONAL_CHINESE)) {
                bean.setName(LanguageConstants.TRADITIONAL_CHINESE_VALUE);
                bean.setNameType(LanguageConstants.TRADITIONAL_CHINESE);
            } else if (defultLanguage[i].equals(LanguageConstants.LITHUANIAN)) {
                bean.setName(LanguageConstants.LITHUANIAN_VALUE);
                bean.setNameType(LanguageConstants.LITHUANIAN);
            } else if (defultLanguage[i].equals(LanguageConstants.ESTONIAN)) {
                bean.setName(LanguageConstants.ESTONIAN_VALUE);
                bean.setNameType(LanguageConstants.ESTONIAN);
            } else if (defultLanguage[i].equals(LanguageConstants.LATVIAN)) {
                bean.setName(LanguageConstants.LATVIAN_VALUE);
                bean.setNameType(LanguageConstants.LATVIAN);
            } else if (defultLanguage[i].equals(LanguageConstants.POLISH)) {
                bean.setName(LanguageConstants.POLISH_VALUE);
                bean.setNameType(LanguageConstants.POLISH);
            } else if (defultLanguage[i].equals(LanguageConstants.ROMANIA)) {
                bean.setName(LanguageConstants.ROMANIA_VALUE);
                bean.setNameType(LanguageConstants.ROMANIA);
            } else if (defultLanguage[i].equals(LanguageConstants.RUSSIAN)) {
                bean.setName(LanguageConstants.RUSSIAN_VALUE);
                bean.setNameType(LanguageConstants.RUSSIAN);
            } else if (defultLanguage[i].equals(LanguageConstants.UKRAINE)) {
                bean.setName(LanguageConstants.UKRAINE_VALUE);
                bean.setNameType(LanguageConstants.UKRAINE);
            }else if (defultLanguage[i].equals(LanguageConstants.SLOVAK)) {
                bean.setName(LanguageConstants.SLOVAK_VALUE);
                bean.setNameType(LanguageConstants.SLOVAK);
            }else if (defultLanguage[i].equals(LanguageConstants.SPANISH)) {
                bean.setName(LanguageConstants.SPANISH_VALUE);
                bean.setNameType(LanguageConstants.SPANISH);
            }else if (defultLanguage[i].equals(LanguageConstants.GERMAN)) {
                bean.setName(LanguageConstants.GERMAN_VALUE);
                bean.setNameType(LanguageConstants.GERMAN);
            }else if (defultLanguage[i].equals(LanguageConstants.ITALIAN)) {
                bean.setName(LanguageConstants.ITALIAN_VALUE);
                bean.setNameType(LanguageConstants.ITALIAN);
            }else if (defultLanguage[i].equals(LanguageConstants.FRENCH)) {
                bean.setName(LanguageConstants.FRENCH_VALUE);
                bean.setNameType(LanguageConstants.FRENCH);
            }else if (defultLanguage[i].equals(LanguageConstants.CZECH)) {
                bean.setName(LanguageConstants.CZECH_VALUE);
                bean.setNameType(LanguageConstants.CZECH);
            }
            else if (defultLanguage[i].equals(LanguageConstants.HUNGARY)) {
                bean.setName(LanguageConstants.HUNGARY_VALUE);
                bean.setNameType(LanguageConstants.HUNGARY);
            }
            mDataList.add(bean);
        }
        LogUtil.e("------------","-----------mDataList---size-:"+mDataList.size());
        setDefaultSelectLanguage();
    }

    @Override
    public void initVariable() {

    }

    @Override
    public void setListener() {

    }

    @Override
    public void initData() {
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back:
                finish();
                break;
        }

    }


    private void setDefaultSelectLanguage() {
        String selectedLanguage =  SpUtils.getString(Constant.SpKeyParam.APP_LANUAGE, Configs.DEFUTL_LANGUAGE);
        for (int n = 0; n < mDataList.size(); n++) {
            if (mDataList.get(n).getNameType().equals(selectedLanguage)) {
                mDataList.get(n).setSelect(true);
            } else {
                mDataList.get(n).setSelect(false);
            }
        }
        mAdapter.refreshDataList(mDataList);
    }
}
